<?php 
class ShopProvider
{
	
	function addShopProvider($title, $title_import)
	{
		if($title != '')
		{
			$insert  = 'INSERT INTO '.DB_PERFIX.'product_providers (`title`, `title_import`)'; 
			$insert .= 'VALUES ("'.$title.'", "'.$title_import.'");';
			$result = mysql_query($insert);
			if ($result > 0)
			{
				$result = mysql_query("SELECT id, title, title_import FROM ".DB_PERFIX."product_providers WHERE title LIKE '".$title."' ORDER BY id DESC LIMIT 1;");
				while($row = mysql_fetch_array($result))
				{
					$item['id'] 			= $row['id'];
					$item['title'] 			= $row['title'];
					$item['title_import'] 	= $row['title_import'];
				}
				return $item;
			}
		}
	}

	function getShopProviders()
	{
		$i=0;
		$result = mysql_query("SELECT * FROM ".DB_PERFIX."product_providers;");
		if(mysql_num_rows($result) > 0)
		{
			while($row = mysql_fetch_array($result))
			{
				foreach(array_keys($row) as $key)
				{ $item[$i][$key] = $row[$key]; }
				$i++;
			}
			return $item;
		}
	}
	
	function getShopProvider($id)
	{
		if($id > 0)
		{
			$result = mysql_query('SELECT * FROM '.DB_PERFIX.'product_providers WHERE id='.$id.';');
			if(mysql_num_rows($result) > 0)
			{
				while($row = mysql_fetch_array($result))
				{
					foreach(array_keys($row) as $key)
					{ $item[$key] = $row[$key]; }
				}
				return $item;
			}
		}
	}
	
	function addBrandToProvider($provider, $brand)
	{
		global $db;
		if(($provider > 0) && ($brand > 0))
		{ $error['add_brand'] = $db->addTablesCombination('provider_id', $provider, 'brand_id', $brand, 'product_provider_to_brands'); }
	}
	
	function remBrandToProvider($provider, $brand)
	{
		global $db;
		if(($provider > 0) && ($brand > 0))
		{ $error['rem_brand'] = $db->remTablesCombination('provider_id', $provider, 'brand_id', $brand, 'product_provider_to_brands'); }
	}
	
	function getShopProviderBrands($provider_id)
	{
		if($provider_id > 0)
		{
			$i=0;
			
			$select = ' SELECT brn.id, brn.title FROM '.DB_PERFIX.'product_brands AS brn ';
			$select = $select.' LEFT JOIN '.DB_PERFIX.'product_provider_to_brands AS ptb ON ptb.brand_id=brn.id ';
			$select = $select.' WHERE ptb.provider_id='.$provider_id.' ORDER BY brn.title ASC;';
			
			$result = mysql_query($select);
			if(mysql_num_rows($result) > 0)
			{
				while($row = mysql_fetch_array($result))
				{
					foreach(array_keys($row) as $key)
					{ $item[$i][$key] = $row[$key]; }
					$i++;
				}
				return $item;
			}
		}
	}
	
	function getShopProviderNotBrands($provider_id)
	{
		if($provider_id > 0)
		{
			$i=0;
			
			$select = ' SELECT brn.id, brn.title FROM '.DB_PERFIX.'product_brands AS brn ';
			$select = $select.' LEFT JOIN '.DB_PERFIX.'product_provider_to_brands AS ptb ON ptb.brand_id<>brn.id ';
			$select = $select.' WHERE ptb.provider_id='.$provider_id.' ORDER BY brn.title ASC;';
			
			$result = mysql_query($select);
			if(mysql_num_rows($result) > 0)
			{
				while($row = mysql_fetch_array($result))
				{
					foreach(array_keys($row) as $key)
					{ $item[$i][$key] = $row[$key]; }
					$i++;
				}
				
			}
			return $items;
		}
	}
}

?>